<template>
  <view>
    <!--搜索框  -->
    <view class="my-search-box">
      <my-search
        bgColor="#0000FF"
        radius="10rpx"
        text="搜索你要的..."
        @click="goToSearchList"
      ></my-search>
    </view>
    <!-- 轮播图 -->
    <swiper
      class="top-swiper"
      circular
      :indicator-dots="true"
      :autoplay="true"
      :interval="3000"
      :duration="1000"
    >
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <navigator
          class="swiper-content"
          :url="`/pages_sub1/product-detail/product-detail?pid=${item.goods_id}`"
        >
          <image :src="item.image_src" mode="scaleToFill" />
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view
        class="nav-item"
        v-for="(item, index) in navList"
        :key="index"
        @click="handleNavClick(item)"
      >
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧 1 个大图 -->
          <view class="left-img-box">
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
              @click="gotoProductList(item.product_list[0])"
            ></image>
          </view>
          <!-- 右侧 4 个小图 -->
          <view class="right-img-box">
            <block v-for="(product, idx) in item.product_list" :key="idx">
              <view class="right-img-item" v-if="idx !== 0">
                <image
                  :src="product.image_src"
                  :style="{ width: product.image_width + 'rpx' }"
                  mode="widthFix"
                  @click="gotoProductList(product)"
                ></image>
              </view>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import badge from "@/mixins/badge";
import MySearch from "@/components/my-search.vue";
export default {
  mixins: [badge],
  components: { MySearch },
  data() {
    return {
      // 1. 轮播数据
      swiperList: [],
      // 2. 导航数据
      navList: [],
      // 3. 楼层数据
      floorList: [],
    };
  },
  created() {
    this.getSwiperList();
    this.getNavList();
    this.getFloorList();
  },
  methods: {
    // 1. 获取轮播图数据
    async getSwiperList() {
      const { meta, message } = await this.$http.get("/home/swiperdata");
      // 1.1 提示用户
      if (meta.status !== 200) return this.$msg("获取轮播图数据失败");
      // 1.2 更新数据
      this.swiperList = message;
      // console.log(this.swiperList);
    },
    // 2. 获取导航数据
    async getNavList() {
      const { meta, message } = await this.$http.get("/home/catitems");
      // 1.1 提示用户
      if (meta.status !== 200) return this.$msg("获取导航数据失败");
      // 1.2 更新数据
      this.navList = message;
      // console.log(this.navList);
    },
    // 3. 点击导航
    handleNavClick(item) {
      // 3.1 判断到分类页面
      if (item.name === "分类") {
        uni.switchTab({
          url: "/pages/category/category",
        });
      }
    },
    // 4. 获取楼层数据
    async getFloorList() {
      const { meta, message } = await this.$http.get("/home/floordata");
      // 1.1 提示用户
      if (meta.status !== 200) return this.$msg("获取楼层数据失败");
      // 1.2 更新数据
      this.floorList = message;
      // console.log(this.floorList);
    },
    // 5. 去商品列表页面
    gotoProductList(item) {
      // console.log(item);
      // 5.1 获取传递的参数
      const query = item.navigator_url.split("?")[1];
      // 5.2 跳转
      uni.navigateTo({
        url: "/pages_sub1/product-list/product-list?" + query,
      });
    },
    // 6. 去搜素页面
    goToSearchList() {
      // console.log("去搜索页面");
      uni.navigateTo({
        url: "/pages_sub1/search/search",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.my-search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}

.top-swiper {
  height: 330rpx;

  .swiper-content,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 30rpx 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  display: flex;
  width: 100%;
  height: 60rpx;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>